package cn.gailvlun.gll.presentation.apartment.task;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.databinding.ActivityModifyTaskBinding;
import cn.gailvlun.gll.db.TaskDb;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.apartment.room.Task;
import cn.gailvlun.gll.net.apartment.template.AddTemplateReq;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.functions.Consumer;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class ModifyTaskActivity extends BaseActivity {

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, ModifyTaskActivity.class);
        context.startActivity(intent);
    }

    public static void openActivity(Activity context, Task task, Consumer<Result<Activity>> consumer) {
        Intent intent = new Intent(context, ModifyTaskActivity.class);
        if (task != null) {
            intent.putExtra("taskId", task.getId());
        }
        RxActivityResult.on(context)
                .startIntent(intent)
                .subscribe(consumer);
    }

    private ActivityModifyTaskBinding mBinding;
    private Task mTask;
    private boolean isAdd;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_modify_task);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.nbv.setOnMenuClickListener(new NavigationBarView.OnMenuClickListener() {
            @Override
            public void onMenuClick(NavigationBarView nb) {
                if (isAdd) {
                    
                    if (IsEmpty.string(mBinding.etTitle.getText()) || IsEmpty.string(mBinding.etContent.getText())) {
                        ToastUtil.showLong("请正确填写任务");
                    } else {
                        addNewTask();
                    }
                } else {




                    if (IsEmpty.string(mBinding.etTitle.getText()) || IsEmpty.string(mBinding.etContent.getText())) {
                        ToastUtil.showLong("请正确填写任务");
                    } else {
                        modifyTask();
                    }
                }
            }
        });
    }

    private void addNewTask() {
        HttpMethods.getTemplateService()
                .addTemplate(new AddTemplateReq(mBinding.etTitle.getText().toString(), mBinding.etContent.getText().toString()))
                .compose(RxUtil.<Task>applyScheduler())
                .subscribe(new HttpSubscriber<Task>() {
                    @Override
                    protected void onSuccess(Task response) {

                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable Task response, int code) {
                        DialogUtil.showError(mContext, errMsg);
                    }
                });
    }

    private void modifyTask() {
        HttpMethods.getTemplateService()
                .modifyTemplate(new AddTemplateReq(mTask.getId(), mBinding.etTitle.getText().toString(),
                        mBinding.etContent.getText().toString()))
                .compose(RxUtil.<Task>applyScheduler())
                .subscribe(new HttpSubscriber<Task>() {
                    @Override
                    protected void onSuccess(Task response) {
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable Task response, int code) {
                        DialogUtil.showError(mContext, errMsg);
                    }
                });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {

        Integer taskId = getIntent().getIntExtra("taskId", -1);
        mTask = TaskDb.getInstance().getTaskById(taskId);
        if (mTask == null) {
            isAdd = true;
            mTask = new Task();
        }
        mBinding.setTasks(mTask);

        if (isAdd) {
            mBinding.nbv.setTitleText("新建任务");
        }
    }
}
